package com.example.myapplication.service;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * 保存当前登录用户信息
 */
public class SharedPreferencesService {

    private static final String USER_INFO_KEY = "userInfo";

    /**
     * 存入数据
     *
     * @param context 应用程序环境的对象
     * @param userId      用户id
     * @param account      账号
     * @param password      密码
     * @return boolean
     */
    public static boolean saveUserInfo(Context context, Integer userId, String account, String password) {
        // 获取SharedPreferences对象
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO_KEY, Context.MODE_PRIVATE);
        // 获取SharedPreferences.Editor对象
        SharedPreferences.Editor editor = preferences.edit();
        // 存储userInfo数据
        editor.putString("userId", String.valueOf(userId));
        editor.putString("account", account);
        editor.putString("password", password);
        // 提交修改
        editor.apply();
        return true;
    }

    /**
     * 更新数据
     *
     * @param context 应用程序环境的对象
     * @param account      账号
     * @param password      密码
     * @return boolean
     */
    public static boolean updateUserInfo(Context context, String account, String password) {
        // 获取SharedPreferences对象
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO_KEY, Context.MODE_PRIVATE);
        // 获取SharedPreferences.Editor对象
        SharedPreferences.Editor editor = preferences.edit();
        // 存储userInfo数据
        editor.putString("account", account);
        editor.putString("password", password);
        // 提交修改
        editor.apply();
        return true;
    }

    /**
     * 读取数据
     * @param context 应用程序环境的对象
     * @return Map
     */
    public static Map<String, Object> getUserInfo(Context context) {
        // 获取SharedPreferences对象
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO_KEY, Context.MODE_PRIVATE);
        // 读取userInfo数据
        String userId = preferences.getString("userId", "");
        String account = preferences.getString("account", "");
        String password = preferences.getString("password", "");
        // 封装到Map集合中
        Map<String, Object> userMap = new HashMap<>();
        if (userId != null && !userId.isEmpty() && !"null".equals(userId)) {
            userMap.put("userId", Integer.parseInt(userId));
        }
        userMap.put("account", account);
        userMap.put("password", password);
        return userMap;
    }

    /**
     * 删除数据
     * @param context 应用程序环境的对象
     * @return boolean
     */
    public static boolean deleteUserInfo(Context context){
        // 获取SharedPreferences对象
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO_KEY, Context.MODE_PRIVATE);
        // 获取SharedPreferences.Editor对象
        SharedPreferences.Editor editor = preferences.edit();
        // 删除userInfo数据
        editor.remove("userId");
        editor.remove("account");
        editor.remove("password");
        // 提交修改
        editor.apply();
        return true;
    }
}
